package com.hucc.api.service;

import com.alibaba.fastjson.JSON;
import com.hucc.api.domain.entity.Group;
import com.hucc.api.domain.entity.Project;
import com.hucc.api.domain.enums.GroupTypeEnum;
import com.hucc.api.domain.exceptions.BusinessException;
import com.hucc.api.domain.req.GroupReq;
import com.hucc.api.repository.GroupMapper;
import com.hucc.api.repository.ProjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GroupService {
    @Autowired
    GroupMapper groupMapper;

    @Autowired
    ProjectMapper projectMapper;

    public void add(GroupReq groupReq) throws BusinessException {
        if (groupReq == null) {
            throw new BusinessException("参数不能为空");
        }

        if (StringUtils.isEmpty(groupReq.getName())) {
            throw new BusinessException("分组名称不能为空");
        }

        if (groupReq.getName().length() > 50) {
            throw new BusinessException("分组名称长度不能超过50");
        }

        if (groupReq.getProjectId() == null) {
            throw new BusinessException("项目ID不能为空");
        }

        Project project = projectMapper.selectByPrimaryKey(groupReq.getProjectId());
        if (project == null) {
            throw new BusinessException("项目不存在");
        }

        List<Group> groups = groupMapper.getByProjectAndName(groupReq.getProjectId(), groupReq.getName());
        if (!groups.isEmpty()) {
            throw new BusinessException("分组名称已存在");
        }

        Group group = new Group();
        group.setName(groupReq.getName());
        group.setProjectId(groupReq.getProjectId());
        group.setType(GroupTypeEnum.CUSTOM.getType());
        group.setCreatetime(new Date());
        group.setUpdatetime(new Date());
        try {
            groupMapper.insertSelective(group);
        } catch (Exception e) {
            log.error("add group error, groupReq: {}", JSON.toJSONString(groupReq), e);
            throw new BusinessException("添加分组到数据库失败");
        }
    }

    public Long add(Long projectId, String groupName) throws BusinessException {
        List<Group> groups = groupMapper.getByProjectAndName(projectId, groupName);
        if (groups.isEmpty()) {
            Group group = new Group();
            group.setName(groupName);
            group.setProjectId(projectId);
            group.setType(GroupTypeEnum.CUSTOM.getType());
            group.setCreatetime(new Date());
            group.setUpdatetime(new Date());
            try {
                groupMapper.insertSelective(group);
                return group.getId();
            } catch (Exception e) {
                log.error("add group error, projectId: {}, groupName: {}", projectId, groupName, e);
                throw new BusinessException("添加分组到数据库失败");
            }
        }
        return groups.get(0).getId();
    }
}
